import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout/Index.vue'
import Login from '@/views/login/Index.vue'
import Home from '@/views/home/Home.vue'
// 用户
const User = ()=>import('@/views/user/Index.vue');
const Dept = ()=>import('@/views/user/dept/Index.vue');
const Position = ()=>import('@/views/user/position/Index.vue');
const Member = ()=>import('@/views/user/member/Index.vue');
//任务
const Task = ()=>import('@/views/task/Index.vue');
const Manage = ()=>import('@/views/task/manage/Index.vue');
const Mine = ()=>import('@/views/task/mine/Index.vue');
//日程
const Schedule = ()=>import('@/views/schedule/Index.vue');
const ManageSchedule = ()=>import('@/views/schedule/manage/Index.vue');
const MineSchedule = ()=>import('@/views/schedule/mine/Index.vue');
const Boardroom = ()=>import('@/views/schedule/boardroom/Index.vue');
//公告
const Notice = ()=>import('@/views/notice/Index.vue');
const List = ()=>import('@/views/notice/list/Index.vue');
const ManageNotice = ()=>import('@/views/notice/manage/Index.vue');
//考勤
const Attendance = ()=>import('@/views/attendance/Index.vue')

const AttendanceList = ()=>import('@/views/attendance/list/Index.vue')
const AttendanceManage = ()=>import('@/views/attendance/manage/Index.vue')
const Vacation = ()=>import('@/views/attendance/vacation/Index.vue')
//数据统计与报表
const Report = ()=>import('@/views/report/Index.vue')
const ReportList = ()=>import('@/views/report/report/Index.vue')
//审批
const Approve = ()=>import('@/views/approve/Index.vue')
const ApproveNoticeAndVacation = ()=>import('@/views/approve/approve/Index.vue')
//文件
const File = ()=>import('@/views/file/Index.vue')
const FileManage = ()=>import('@/views/file/manage/Index.vue')
//自动回复机器人
const Robot = ()=>import('@/views/robot/Index.vue')
const AutoAnswer = ()=>import('@/views/robot/autoanswer/Index.vue')
Vue.use(VueRouter)

  
import store from '@/store'; // 导入 Vuex store  

Vue.use(VueRouter);  

const routes = [
  {
    path:'/',
    component:Layout,
    children:[
      {
        path:'/',
        name:'home',
        component:Home,
        meta: { requiresAuth: true } // 需要身份验证
      },{
        path:'/user',
        name:'user',
        component:User,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'dept',//访问路径/user/dept
            name:'dept',
            component:Dept,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'position',
            name:'position',
            component:Position,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'member',
            name:'member',
            component:Member,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/task',
        name:'task',
        component:Task,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'manage',
            name:'manage',
            component:Manage,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'mine',
            name:'mine',
            component:Mine,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/schedule',
        name:'schedule',
        component:Schedule,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'manageSchedule',
            name:'manageSchedule',
            component:ManageSchedule,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'mineSchedule',
            name:'mineSchedule',
            component:MineSchedule,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'boardroom',
            name:'boardroom',
            component:Boardroom,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/notice',
        name:'notice',
        component:Notice,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'list',
            name:'list',
            component:List,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'manageNotice',
            name:'noticeManage',
            component:ManageNotice,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/attendance',
        name:'attendance',
        component:Attendance,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[{
            path:'attendanceList',
            name:'attendanceList',
            component:AttendanceList,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'attendanceManage',
            name:'attendanceManage',
            component:AttendanceManage,
            meta: { requiresAuth: true }, // 需要身份验证
          },{
            path:'vacation',
            name:'vacation',
            component:Vacation,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/report',
        name:'report',
        component:Report,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[{
          path:'reportList',
          name:'reportList',
          component:ReportList,
          meta: { requiresAuth: true }, // 需要身份验证
        }]
      },{
        path:'/approve',
        name:'approve',
        component:Approve,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[{
          path:'approveNoticeAndVacation',
          name:'approveNoticeAndVacation',
          component:ApproveNoticeAndVacation,
          meta: { requiresAuth: true }, // 需要身份验证
        }]
      },{
        path:'/file',
        name:'file',
        component:File,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'fileManage',
            name:'fileManage',
            component:FileManage,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      },{
        path:'/robot',
        name:'robot',
        component:Robot,
        meta: { requiresAuth: true }, // 需要身份验证
        children:[
          {
            path:'autoanswer',
            name:'autoanswer',
            component:AutoAnswer,
            meta: { requiresAuth: true }, // 需要身份验证
          }
        ]
      }
    ]
  },{
    path:'/login',
    name:'login',
    component:Login
  }

]

const router = new VueRouter({
  routes
})

// 路由守卫  
router.beforeEach((to, from, next) => {  
  const isAuthenticated = store.state.isAuthenticated; // 获取认证状态  
  console.log("是否认证",isAuthenticated)
  // 如果目标路由需要身份验证且用户未认证  
  if (to.meta.requiresAuth && !isAuthenticated) {  
    next('/login'); // 重定向到登录页  
  } else {  
    next(); // 允许访问  
  }  
});  

export default router
